<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Progressive Play Server
 * Weapon Stats Model
 * /models/weapon_model.php
 * By: Yukwunhang
 */
class Weapon_model extends Model
{
	// Constructor
	function Weapon_model()
	{
		parent::Model();
		// Load helpers
		$this->load->helper('array');
	}
	/**
	 * Get weapon usage stats
	 * @return weapon usage sorted from most to least used
	 * @param object $sort_key[optional]
	 */
	function get_weapon_stats($sort_key = 'kills')
	{
		$this->db->select('weapon, kills');
		$this->db->from('weapons');
		$this->db->order_by($sort_key, 'desc');
		
		// Run query
		$query = $this->db->get();
		$result = $query->result_array();
		
		// Fix Selfkill <-> USSOCOM naming bug
		foreach ($result as $key => $item)
		{
			if ($result[$key]['weapon'] == 'Selfkill')
			{
				$result[$key]['weapon'] = 'USSOCOM';
			}
		}
		
		return $result;
	}
	/**
	 * Get top weapon users.
	 * @return List of top users of a weapon with number of kills with said weapon.
	 * @param object $weapon
	 * @param object $number[optional]
	 * @param object $offset[optional]
	 */
	function get_top_weapon_users($weapon, $number = 10, $offset = 0)
	{
		// Fix Selfkill <-> USSOCOM naming bug
		if ($weapon == 'USSOCOM')
		{
			$weapon = 'Selfkill';
		}
		
		/* CI's active record doesn't work when there
		 * are spaces in field names.
		 * -> Write the query manually
		 */
		$query = $this->db->query("SELECT `$weapon`, user_id, name
									FROM players
									ORDER BY `$weapon` DESC
									LIMIT $offset, $number");
									
		$result = $query->result_array();
		
		// Change the array keys '$weapon' to 'kills' to avoid confusion
		// and fix the naming bug.
		$result = array_change_key_name($weapon, 'kills', $result);
		
		return $result;
	}
}
/*
 * End of file
 */